﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Scraper
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.folderBrowserDialog1.SelectedPath = @"c:\";
            UpdateSelectedDirectoryInfo(this.folderBrowserDialog1.SelectedPath);
        }

        public void UpdateSelectedDirectoryInfo(string path)
        {
            this.folderBrowserDialog1.SelectedPath = path;
            this.txtDirectory.Text = path;
            var directoryInfo = new DirectoryInfo(path);
            this.fileList.Items.Clear();


            foreach (var a in directoryInfo.GetDirectories())
            {
                var item = fileList.Items.Add(a.Name);
                item.Tag = a;
                item.ImageIndex = 1;
            }

            var files = directoryInfo.GetFiles().AsEnumerable();
            if (rbOrderByCreated.Checked)
                files = files.OrderBy(a => a.CreationTime);
            else if (rbOrderByName.Checked)
                files = files.OrderBy(a => a.Name);
            foreach (var a in files)
            {
                var item = fileList.Items.Add(a.Name);
                item.Tag = a;
                item.ImageIndex = 0;
            }


        }

        private void fileList_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        private void fileList_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            var path = string.Format("{0}\\{1}", folderBrowserDialog1.SelectedPath, fileList.SelectedItems[0].Text);
            UpdateSelectedDirectoryInfo(path);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            var info = new DirectoryInfo(folderBrowserDialog1.SelectedPath);
            UpdateSelectedDirectoryInfo(info.Parent.FullName);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                var season = (int)udSeason.Value;
                var episode = (int)udEpisode.Value;
                foreach (ListViewItem item in fileList.SelectedItems)
                {
                    var fileInfo = item.Tag as FileInfo;
                    string newName = string.Format("{0}\\{1} s{2}e{3}{4}", fileInfo.Directory, txtFileName.Text,
                                                   season.ToString("D2"), episode.ToString("D2"), fileInfo.Extension);
                    File.Move(fileInfo.FullName, newName);
                    episode++;
                }
                UpdateSelectedDirectoryInfo(folderBrowserDialog1.SelectedPath);
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog(this) == DialogResult.OK)
            {
                UpdateSelectedDirectoryInfo(folderBrowserDialog1.SelectedPath);
            }
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            UpdateSelectedDirectoryInfo(folderBrowserDialog1.SelectedPath);
        }
    }
}
